package com.example.demo.controller;

import com.example.demo.config.MyRateLimiter;
import com.example.demo.entity.Tran;
import com.example.demo.service.TranService;
import com.example.demo.utils.JwtUtils;
import com.example.demo.utils.Result;
import com.example.demo.utils.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class TranController {

    @Autowired
    MyRateLimiter myRateLimiter;

    @Autowired
    TranService tranService;
    ResultCode resultCode = new ResultCode();

    @GetMapping("/tranList")
    public Result List(@RequestParam(required = false, value = "num") String num) {
        Result res = new Result();

        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
        //尝试获取令牌
        boolean acquire = myRateLimiter.tryAccountRegisterAcquire();
        System.out.println(Thread.currentThread().getName() + " tranList 尝试获取令牌结果"+acquire);
        if (acquire) {
            System.out.println(num);
            Map<String, Object> map = new HashMap<String, Object>();
            try {
                List<Tran> list = tranService.getList(num);
                map.put("list", list);
                res.setMessage("返回成功");
                res.setData(map);
            } catch (Exception e) {
                e.printStackTrace();
                res.setCode(resultCode.ERROR);
                res.setMessage(resultCode.getMsg(resultCode.ERROR));
            }
            System.out.println(Thread.currentThread().getName()+" tranList 成功拿到令牌 [" +  dtf.format(LocalDateTime.now()) + "]");
            return res;
        }
        System.out.println(Thread.currentThread().getName()+" tranList 被限制了 [" +  dtf.format(LocalDateTime.now())+ "]");
        return res;
    }

    @PostMapping("/addTran")
    public Result register(@RequestBody Tran tran) {
        Result res = new Result();

        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
        //尝试获取令牌
        boolean acquire = myRateLimiter.tryAccountRegisterAcquire();
        System.out.println(Thread.currentThread().getName() + " addTran 尝试获取令牌结果"+acquire);
        if (acquire) {
            try {
                System.out.println("transportation: " + tran);
                int r = tranService.register(tran);
                if (r > 0) {
                    res.setMessage("添加订单成功");
                } else {
                    res.setCode(ResultCode.REGERROR);
                    res.setMessage(resultCode.getMsg(ResultCode.REGERROR));
                }
            } catch (Exception e) {
                e.printStackTrace();
                res.setCode(ResultCode.ERROR);
                res.setMessage(resultCode.getMsg(ResultCode.ERROR));
            }
            System.out.println(Thread.currentThread().getName()+" addTran 成功拿到令牌 [" +  dtf.format(LocalDateTime.now()) + "]");
            return res;
        }
        System.out.println(Thread.currentThread().getName()+" addTran 被限制了 [" +  dtf.format(LocalDateTime.now())+ "]");
        return res;
    }

    @PutMapping("/updateTran")
    public Result update(@RequestBody Tran tran){
        Result res = new Result();

        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
        //尝试获取令牌
        boolean acquire = myRateLimiter.tryAccountRegisterAcquire();
        System.out.println(Thread.currentThread().getName() + " updateTran 尝试获取令牌结果"+acquire);
        if (acquire) {
            try {
                int r = tranService.update(tran);
                if(r > 0){
                    res.setMessage("订单信息更新成功");
                }else{
                    res.setCode(ResultCode.UPDATEERROR);
                    res.setMessage(resultCode.getMsg(ResultCode.UPDATEERROR));
                }
            } catch (Exception e) {
                e.printStackTrace();
                res.setCode(ResultCode.ERROR);
                res.setMessage(resultCode.getMsg(ResultCode.ERROR));
            }
            System.out.println(Thread.currentThread().getName()+" updateTran 成功拿到令牌 [" +  dtf.format(LocalDateTime.now()) + "]");
            return res;
        }
        System.out.println(Thread.currentThread().getName()+" updateTran 被限制了 [" +  dtf.format(LocalDateTime.now())+ "]");
        return res;
    }

    @DeleteMapping("/deleteTran")
    public Result delete(@RequestParam(required = true,value = "id") Integer id){
        Result res = new Result();

        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
        //尝试获取令牌
        boolean acquire = myRateLimiter.tryAccountRegisterAcquire();
        System.out.println(Thread.currentThread().getName() + " deleteTran 尝试获取令牌结果"+acquire);
        if (acquire) {
            try {
                System.out.println("id===="+id);
                int r = tranService.delete(id);
                if(r>0){
                    res.setMessage("订单信息删除成功");
                }else {
                    res.setCode(ResultCode.DELETEERROR);
                    res.setMessage(resultCode.getMsg(ResultCode.DELETEERROR));
                }
            }catch (Exception e){
                e.printStackTrace();
                res.setCode(ResultCode.ERROR);
                res.setMessage(resultCode.getMsg(ResultCode.ERROR));
            }
            System.out.println(Thread.currentThread().getName()+" deleteTran 成功拿到令牌 [" +  dtf.format(LocalDateTime.now()) + "]");
            return res;
        }
        System.out.println(Thread.currentThread().getName()+" deleteTran 被限制了 [" +  dtf.format(LocalDateTime.now())+ "]");
        return res;
    }
}
